<?php
class Sectores extends CI_Controller {
    public function __construct() {
        parent::__construct();
        $this->load->helper(array('url'));
        $this->load->library(array('session', 'r_session', 'form_validation', 'email'));
        $this->r_session->check($this->session->all_userdata());
        $this->load->model(array(
            'sectores_model'
        ));
    }
    
    public function index() {
        $session = $this->session->all_userdata();
        $session['title'] = 'Listado de Sectores';
        $data['sectores'] = $this->sectores_model->get_sectores();
        
        $this->load->view('layout/header', $session);
        $this->load->view('sectores/index', $data);
        $this->load->view('layout/footer');
    }
    
    public function update($idsector) {
        $session = $this->session->all_userdata();
        $session['title'] = "Actualizar Sector";
        $data['sector'] = $this->sectores_model->get_sector($idsector);
        
        $this->form_validation->set_rules('sector', 'Sector', 'required');
        
        if($this->form_validation->run() == FALSE) {
            
        } else {
            $datos = array(
                'sector' => $this->input->post('sector')
            );
            
            $this->sectores_model->update($datos, $idsector);
            
            redirect('/sectores/', 'refresh');
        }
        
        $this->load->view('layout/header', $session);
        $this->load->view('sectores/update', $data);
        $this->load->view('layout/footer');
    }
    
    public function agregar() {
        $session = $this->session->all_userdata();
        $session['title'] = "Agregar Sector";
        
        $this->form_validation->set_rules('sector', 'Sector', 'required');
        
        if($this->form_validation->run() == FALSE) {
            
        } else {
            $datos = array(
                'sector' => $this->input->post('sector')
            );
            
            $this->sectores_model->set_sector($datos);
            
            redirect('/sectores/', 'refresh');
        }
        
        $this->load->view('layout/header', $session);
        $this->load->view('sectores/agregar');
        $this->load->view('layout/footer');
    }
}
?>
